package com.guimin.recycle.controller;

import com.guimin.common.util.exception.BaseException;
import com.guimin.common.vo.R;
import com.guimin.recycle.entity.GuiminRecycle;
import com.guimin.recycle.service.GuiminRecycleService;
import com.guimin.recycle.service.impl.GuiminRecycleServiceImpl;
import com.guimin.user.dto.LoginInfoDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (GuiminRecycle)表控制层
 *
 * @author makejava
 * @since 2021-11-24 16:42:32
 */
@RestController
@RequestMapping("Recycle")
public class GuiminRecycleController {
    /**
     * 服务对象
     */
    @Resource
    private GuiminRecycleServiceImpl RecycleService;

    /**redis*/
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 查询垃圾回收服务列表(一级)
     * @param request
     * @return
     */
    @PostMapping("/list/one")
    @Async
    public R<?> getRecycleListNoe(HttpServletRequest request){
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        return RecycleService.getRecycleListNoe(userInfo.getId());
    }

    /**
     * 查询垃圾回收服务列表(二级)
     * @param request
     * @return
     */
    @PostMapping("/list/two")
    @Async
    public R<?> getRecycleListTwo(HttpServletRequest request){
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        return RecycleService.getRecycleListTwo(userInfo.getId());
    }

    /**
     * 添加一级类
     * @param request
     * @param name
     * @return
     */
    @GetMapping("/add/one")
    public R<?> addRecycleNoe(HttpServletRequest request,@RequestParam String name){
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        return RecycleService.addRecycleNoe(userInfo.getId(),name);
    }

    /**
     * 添加二级类
     * @param request
     * @param recycle
     * @return
     */
    @PostMapping("/add/two")
    public R<?> addRecycleTwo(HttpServletRequest request,@RequestBody GuiminRecycle recycle){
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        return RecycleService.addRecycleTwo(userInfo.getId(),recycle);
    }

    /**
     * 按id删除字段
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R<?> deleteRecycle(HttpServletRequest request,@RequestParam Long id){
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        return RecycleService.deleteRecycle(userInfo.getId(),id);
    }

    /**
     * 编辑二级类
     * @param recycle
     * @return
     */
    @PostMapping("/alter/two")
    public R<?> alterRecycle(@RequestBody GuiminRecycle recycle){
        if(recycle == null){
            throw new BaseException("请求参数为空");
        }
        return RecycleService.alterRecycle(recycle);
    }

}

